import openai
import logging

import requests


def qwen_intent_recognition(text: str, conversation_history):
    openai.api_base = "http://localhost:8000/v1"
    openai.api_key = "none"
    conversation_history.append({"role": "user", "content": text})
    try:
        response = openai.ChatCompletion.create(
            model="Qwen",
            messages=conversation_history,
            stream=False,
            stop=[]
        )
        result = response.choices[0].message.content
        conversation_history.append({"role": "assistant", "content": result})
        return result
    except requests.exceptions.RequestException as e:
        logging.error(f"网络连接出现问题导致意图识别失败: {e}")
        return "网络连接出现问题，请稍后再试"
    except Exception as e:
        logging.error(f"意图识别出现其他错误: {e}")
        return "意图识别出现错误，请检查输入内容"